//
//  HQDiscoverSearchView.m
//  wbOC
//
//  Created by sheji on 16/3/2.
//  Copyright (c) 2016年 sheji. All rights reserved.
//

#import "HQDiscoverSearchView.h"


@interface HQDiscoverSearchView ()<UITextFieldDelegate>
/**
 *  输入框
 */
@property (nonatomic, strong) UITextField *textField;
/**
 *  输入框右侧约束
 */
@property (nonatomic, strong) NSLayoutConstraint *textFieldRightConstraint;
/**
 *  取消按钮
 */
@property (nonatomic, strong) UIButton *cancelButton;
@end

@implementation HQDiscoverSearchView


- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

#pragma mark - 监听方法
- (void)clickCancelButton {
    [self.textField resignFirstResponder];
    
    [self updateTextFieldConstraint:0];
}

#pragma mark - UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    [self updateTextFieldConstraint:-self.cancelButton.bounds.size.width];
}


/**
 *  动画更新文本框右侧约束
 *
 *  @param constraint 约束数值
 */
- (void)updateTextFieldConstraint:(CGFloat)constraint {
    self.textFieldRightConstraint.constant = constraint;
    
    [UIView animateWithDuration:0.25 animations:^{
        [self layoutIfNeeded];
    }];
}

#pragma mark - 设置界面
/// 设置类型
- (void)setupUI {
    
    // 添加控件
    [self addSubview:self.cancelButton];
    [self addSubview:self.textField];
    
    // 自动布局
    NSDictionary *viewDict = NSDictionaryOfVariableBindings(_textField, _cancelButton);
    [viewDict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
        [obj setTranslatesAutoresizingMaskIntoConstraints:NO];
    }];
    
    
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[_cancelButton(45)]-0-|"
                                                                 options:0
                                                                 metrics:nil
                                                                   views:viewDict]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[_cancelButton]-0-|"
                                                                 options:0
                                                                 metrics:nil
                                                                   views:viewDict]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:_textField
                                                     attribute:NSLayoutAttributeLeft
                                                     relatedBy:NSLayoutRelationEqual
                                                        toItem:self
                                                     attribute:NSLayoutAttributeLeft
                                                    multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:_textField
                                                     attribute:NSLayoutAttributeRight
                                                     relatedBy:NSLayoutRelationEqual
                                                        toItem:self
                                                     attribute:NSLayoutAttributeRight
                                                    multiplier:1.0 constant:0]];
    self.textFieldRightConstraint = self.constraints.lastObject;
    
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[_textField]-0-|"
                                                                 options:0
                                                                 metrics:nil
                                                                   views:viewDict]];
}

#pragma mark - 懒加载控件
- (UITextField *)textField {
    if (_textField == nil) {
        _textField = [[UITextField alloc] init];
        
//        _textField.backgroundColor = [UIColor colorWithRed:0.4 green:.8 blue:1.0 alpha:0.5];
        _textField.layer.borderColor = [UIColor grayColor].CGColor;
        _textField.layer.borderWidth = 1.0;
        _textField.layer.cornerRadius = 2.0;
        _textField.layer.masksToBounds = YES;
        
        _textField.placeholder = @"听说敲代码和卫龙辣条更配呦";
        
        //在textField设置占位符,并让它一直出现
//        UILabel *paddingView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 5, 35)];
//        _textField.leftView  = paddingView;
        
        //设置左侧视图
        UIImageView *leftView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"searchbar_textfield_search_icon"]];
        leftView.frame = CGRectMake(0, 0, self.bounds.size.height, self.bounds.size.height);
        _textField.leftView = leftView;
        _textField.leftViewMode = UITextFieldViewModeAlways;
        
        _textField.delegate = self;
    }
    return _textField;
}

- (UIButton *)cancelButton {
    if (_cancelButton == nil) {
        _cancelButton = [[UIButton alloc] init];
        
        [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        
        [self addSubview:_cancelButton];
        
        // 监听方法
        [_cancelButton addTarget:self action:@selector(clickCancelButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelButton;
}

@end
